// icydoor.txt - Modified door.txt... really only used for one door in
//  the entire scenario.

// Memory Cells - 
//   0 - Lock level. If left at 0, door is unlocked. Otherwise, the strength/tool
//     use needed to get it open. If this is set to a really high number (say, 200),
//     it can't be unlocked by normal means
//   1 - Key needed. If left at 0, no special item helps unlock the door. Otherwise,
//     if that party has this special item, the door automatically unlocks.
//   2,3 - Coordinates for a stuff done flag. If these are 0 and 0, ignored. Otherwise,
//     the stuff done flag is set to 1 when the door is unlocked. If the flag is non-zero,
//     than when the party enters this zone, the door will ebcome unlocked.
//   4 - Town script run when the door is successfully opened.

// Notes - 
//   -When SDF (5,6) == 0, town script 40 is run before anything else in the BLOCK_MOVE_STATE is done, and 5,6 is set to 1. 
//   -The cutscene gives this script a message, which prompts the variable frozen to be set to 1. Then, the door cannot be opened until the captain is disrupted.
//   -The captain's death gives the script a message, which sets frozen back to 0. Then, the door can be opened as normal.
//   -If the party tries to open the door without having a crystal, they're warned and given the choice to go for the door.

beginterrainscript; 

variables;
	short i_am_open = 0;
	short cur_terrain;
	short i_am_locked = 0;
	
	short door_opened;
	short choice;

	short frozen = 0;
body;

beginstate INIT_STATE;
	cur_terrain = terrain_in_this_spot();
	if (((cur_terrain >= 14) && (cur_terrain <= 17)) ||
	  ((cur_terrain >= 50) && (cur_terrain <= 53)))
	  	i_am_open = 1;
	  	else i_am_open = 0;

	if (get_memory_cell(0) > 0) {
		i_am_locked = 1;
		set_mechanism_difficulty(get_memory_cell(0));
		set_physical_strength(get_memory_cell(0));
		
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
			if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
				i_am_locked = 0;
			}
		}
		
	break;

beginstate START_STATE;

	if(get_flag(5,13) == 1)
		frozen = 1;

	if(get_flag(5,13) == 2)
		frozen = 0;

break;

beginstate SEARCH_STATE;
	if (i_am_open == 1) {
		print_str_color("You close the door.",2);
		flip_terrain(my_loc_x(),my_loc_y());
		i_am_open = 0;
		play_sound(-59);
		}
break;

beginstate BLOCK_MOVE_STATE;

	if(get_flag(5,6) == 0) {
		if((has_item(446) == 0) && (has_item(447) == 0)) {
			reset_dialog();
			add_dialog_str(0,"This door should lead to the main field generator, assuming that Felix told you correctly. However, you realize that you don't have a control crystal with which to fix the machine.",0);
			add_dialog_str(1,"Do you proceed?",0);
			add_dialog_choice(0,"No.");
			add_dialog_choice(1,"Yes.");
			choice = run_dialog(1);
			if(choice == 1)
				end();
			}
		set_flag(5,6,1);
		frozen = 1;
		block_entry(1);
		run_town_script(40);
		end();
		}

	if(frozen == 1) {
		print_str_color("The door is still frozen shut.",2);
		block_entry(1);
		end();
		}
	else if(get_flag(5,14) < 2) {
		reset_dialog();
		if(get_flag(5,14) == 1)
			add_dialog_str(0,"As you expected, the door has thawed enough for you to take a legitimate shot at opening it.",0);
		else
			add_dialog_str(0,"Surprisingly, the door has thawed out enough that you think you might be able to open it. It probably helped that you disrupted the captain.",0);
		run_dialog(1);
		set_flag(5,14,2);
		}

	if (i_am_open == 0) {
		block_entry(1);
		
		door_opened = 1;

		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
			if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
				i_am_locked = 0;
			}
		
		if ((get_memory_cell(1) > 0) && (i_am_locked > 0)) {
			if (has_special_item(get_memory_cell(1))) {
				print_str_color("You have the key which unlocks this door.",2);
				if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
					set_flag(get_memory_cell(2),get_memory_cell(3),1);
				i_am_locked = 0;
				}
			} 
		if (i_am_locked) {
			reset_dialog();
			if (get_memory_cell(1) > 0)
				add_dialog_str(0,"This door is locked, and you don't have the right key. You can have your strongest character try to bash it down (which requires high strength) or have your most skilled character try to pick the lock (which requires Tool Use skill and a lockpick).",0);
				else add_dialog_str(0,"This door is locked. You can have your strongest character try to bash it down (which requires high strength) or have your most skilled character try to pick the lock (which requires Tool Use skill and a lockpick).",0);
			add_dialog_choice(0,"Leave the door alone.");
			add_dialog_choice(1,"Try to bash it down.");
			add_dialog_choice(2,"Try to pick the lock.");
			choice = run_dialog(0);
			
			if (choice == 1)
				end();
			if (choice == 2) {
				if (run_bash_door(get_physical_strength()) == FALSE)
					door_opened = 0;
				}
			if (choice == 3) {
				if (run_pick_lock(get_mechanism_difficulty()) == FALSE) 
					door_opened = 0;
					else if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
						award_party_xp(BASE_TRAP_XP,1 + 2 * get_mechanism_difficulty());
					
				}
			}
			
		if (door_opened) {
			print_str("You open the door.");
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
				set_flag(get_memory_cell(2),get_memory_cell(3),1);
			flip_terrain(my_loc_x(),my_loc_y());
			i_am_open = 1;
			i_am_locked = 0;
			play_sound(-58);
			if (get_memory_cell(4) != 0)
				run_town_script(get_memory_cell(4));
			}
		}
break;

beginstate UNLOCK_SPELL_STATE;
	if(frozen == 1) {
		print_str_color("Unlock Doors: The door remains frozen shut.",2);
		end();
		}

	if ((i_am_open > 0) || (i_am_locked == 0))
		print_str_color("Unlock Doors: The spell doesn't affect unlocked doors.",2);
		else {
			if (get_unlock_spell_strength() >= get_mechanism_difficulty() * 2) {
				print_str_color("Unlock Doors: The spell unlocks a door.",2);
				i_am_locked = 0;
				play_sound(9);
				if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
					set_flag(get_memory_cell(2),get_memory_cell(3),1);
				}
				else {
					print_str_color("Unlock Doors: The spell isn't strong enough to affect the door.",2);
					print_str_color("  (This spell usually only affects doors with magical protection.)",2);
					}
			}
break;

beginstate DISPEL_BARRIER_STATE;
	if ((i_am_open == 0) && (i_am_locked)) {
		print_str_color("Dispel Barrier: The spell fails to affect a locked door.",2);
		}
break;
